import { Component } from "@angular/core";
import {
  NavController,
  LoadingController,
  AlertController
} from "ionic-angular";
import { Storage } from "@ionic/storage";
import { HttpService } from "../../providers/http.service";
import { appConfig } from "../../providers/config";
import { CommonFunc } from "../../providers/commonFunc";
import { LoginPage } from "../login/login";

@Component({
  templateUrl: "changePhone.html",
  providers: [HttpService, CommonFunc]
})
export class ChangePhonePage {
  private newphone = "";
  constructor(
    public navCtrl: NavController,
    private storage: Storage,
    private httpService: HttpService,
    private alert: AlertController,
    private loading: LoadingController
  ) {

  }

  doSubmit() {

    this.storage.get('user').then(user => {
      if (!this.newphone) {
        this.alert
          .create({
            title: '错误',
            message: '请填写完整',
            buttons: ['确认'],
            enableBackdropDismiss: true
          })
          .present();
        return;
      } else {
        if ((this.newphone + '').split('')[0] != '1') {
          this.alert
            .create({
              title: '错误',
              message: '请输入正确手机号码',
              buttons: ['确认'],
              enableBackdropDismiss: true
            })
            .present();
          return;
        }
        if ((this.newphone + '').length != 11) {
          this.alert
            .create({
              title: '错误',
              message: '请输入11位手机号码',
              buttons: ['确认'],
              enableBackdropDismiss: true
            })
            .present();
          return;

        }
      }

      let param = {
        id: user.id,
        newphone: this.newphone
      }
      this.httpService.updateUserPhone(param).subscribe(data => {
        if (data.status == '200') {
          this.alert
            .create({
              title: '成功',
              message: '修改成功',
              buttons: ['确认'],
              enableBackdropDismiss: true
            })
            .present();
          this.navCtrl.pop();
        } else {

          this.alert
            .create({
              title: '错误',
              message: data.message,
              buttons: ['确认'],
              enableBackdropDismiss: true
            })
            .present();
          return;
        }
      });
    });

  }

  ionViewWillEnter() { }

  cancel() {
    this.navCtrl.pop();
  }
}
